import "./public-path";
import Vue from "vue";
import Antd from "ant-design-vue";
import VueRouter from "vue-router";
import App from "./App.vue";
import routes from "./router";
import actions from "./utils/action";
import request from "./utils/axios";
import "ant-design-vue/dist/antd.css";

import moment from 'moment';

Vue.use(Antd);

// 设置moment的语言为中文
moment.locale('zh-cn');

import store from "./store/index";

Vue.config.productionTip = false;
Vue.prototype.$request = request;

Vue.use(VueRouter);

let router = null;
let instance = null;
function render(props = {}) {
  actions.setAction(props);
  const { container } = props;

  router = new VueRouter({
    base: window.__POWERED_BY_QIANKUN__
      ? process.env.VUE_APP_ROUTE_BASE_PATH
      : "/",
    mode: "hash",
    routes,
    // 全局滚动行为配置（核心解决方案）
    scrollBehavior(to, from, savedPosition) {
      // 处理锚点链接
      if (to.hash) {
        return {
          selector: to.hash,
          offset: { x: 0, y: 60 },
          behavior: 'smooth'
        }
      }
      // 返回顶部（支持前进/后退时恢复位置）
      return savedPosition || { x: 0, y: 0 }
    }
  });

  instance = new Vue({
    router,
    store,
    render: (h) => h(App),
  }).$mount(container ? container.querySelector("#app") : "#app");
}

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

export async function bootstrap() {
  console.log("");
}
export async function mount(props) {
  render(props);
}
export async function unmount() {
  instance.$destroy();
  instance.$el.innerHTML = "";
  instance = null;
  router = null;
}

/**
 * 可选生命周期钩子，仅使用 loadMicroApp 方式加载微应用时生效
 */
export async function update(props) {
  // actions.setCommonData(props);
  // actions.initGlState(props);
}
